home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / A-line / Scripts / MakeImports.pm < prev    next >
Encoding:
Text File  |  2000-06-24  |  4.4 KB  |  157 lines

  1. #!perl -w
  2. # Special MPW glue
  3. undef;
  4.  
  5. package MakeImports;
  6.  
  7. require Exporter;
  8. @ISA = 'Exporter';
  9. @EXPORT = qw(MakeImports);
  10.  
  11. # MakeImports
  12.  
  13. # Note: The TargetDir sub below uses the readlink builtin, which in MacPerl
  14. # constructs a pathname based on the information stored in the alias, without
  15. # actually attempting to resolve it.  This is unfortunate -- especially because
  16. # of, but not as much as, the Finder's neglect to update aliases as it resolves
  17. # them.  Said again:  MacPerl's readlink doesn't resolve aliases, and the Finder
  18. # doesn't update them when it uses them.  So, either we use a MacPerl extension,
  19. # or we make the user fix the aliases (unless we enroll Matthias into enhancing
  20. # readlink).  We make the user do it.
  21. #
  22. # If readlink is producing bad pathnames, then select the problematic aliases
  23. # in the Finder, and run the Recreate Aliases script (AppleScript).  You must do
  24. # this without changing the Finder selection, of course -- you can use the
  25. # Script Editor, or install OSA Menu and put an alias of the script in the Finder
  26. # Scripts folder and run it that way (recommended).
  27.  
  28. use SCF;
  29.  
  30. use strict;
  31.  
  32. Main() unless defined caller;
  33.  
  34. sub Main {
  35.     my $output = MakeImports(@ARGV);
  36.     foreach (@$output) {
  37.         print;
  38.     }
  39. }
  40.  
  41. sub Unique {
  42.     my @list;
  43.     my $item;
  44.     while ($item = shift) {
  45.         push @list, $item unless grep {$_ eq $item} @list;
  46.     }
  47.     return @list;
  48. }
  49.  
  50. sub ResolveDir {
  51.     my ($pathname) = @_;
  52.     my $original = readlink($pathname);
  53.     if (defined $original) {
  54.         -d $original or die "Stale alias '$pathname'";
  55.         return $original;
  56.     } else {
  57.         -d $pathname or die "Missing directory '$pathname'";
  58.         return $pathname;
  59.     }
  60. }
  61.  
  62. sub TargetDir {
  63.     my ($target, $dir) = @_;
  64.     # Assumes MPW environment
  65.     $dir ||= "Menu";
  66.     my ($pathname, $original);
  67.     $pathname = ResolveDir("$ENV{Boot}Development");
  68.     $pathname = ResolveDir("$pathname:$dir");
  69.     $pathname = ResolveDir("$pathname:$target");
  70.     return "$pathname:"
  71. }
  72.  
  73. sub MakeImports {
  74.     my $scf = SCF::Read(@_);
  75.     my %dirs;
  76.     my @source;
  77.     my (@includes, @sysincludes);
  78.     my %libraries;
  79.     my @resources;
  80.     foreach my $section (SCF::Sections($scf)) {
  81.         my $srcs = SCF::Lookup($scf, $section, 'Sources');
  82.         if (defined $srcs) {
  83.             foreach my $src (split ' ', $srcs) {
  84.                 push @source, $src;
  85.                 my ($ext) = $src =~ /\.(\w+?)$/;
  86.                 push @{$dirs{$ext}}, TargetDir($section) . "Sources:";
  87.             }
  88.         }
  89.         my $incs = SCF::Lookup($scf, $section, 'Include');
  90.         if (defined $incs) {
  91.             foreach my $inc (split ' ', $incs) {
  92.                 my ($ext) = $inc =~ /\.(\w+?)$/;
  93.                 my $dir = TargetDir($section) . "Includes:";
  94.                 push @{$dirs{$ext}}, $dir;
  95.                 push @includes, "$dir";
  96.             }
  97.         }
  98.         my $sysincs = SCF::Lookup($scf, $section, 'SysInclude');
  99.         if (defined $sysincs) {
  100.             foreach my $sysinc (split ' ', $sysincs) {
  101.                 my ($ext) = $sysinc =~ /\.(\w+?)$/;
  102.                 my $dir = TargetDir($section) . "Includes:";
  103.                 push @{$dirs{$ext}}, $dir;
  104.                 push @sysincludes, "$dir";
  105.             }
  106.         }
  107.         my $libs = SCF::Lookup($scf, $section, 'Libraries');
  108.         if (defined $libs) {
  109.             foreach my $lib (split ' ', $libs) {
  110.                 my $dir = TargetDir($section, "Lab") 
  111.                     . '(Libraries):$(ARCH):$(COMPILER):$(BUILD):';
  112.                 push @{$libraries{''}}, "$dir$lib";
  113.             }
  114.         }
  115.         my $syslibs = SCF::Lookup($scf, $section, 'SysLibraries');
  116.         if (defined $syslibs) {
  117.             my $arch = SCF::Lookup($scf, $section, 'Arch');
  118.             my $compiler = SCF::Lookup($scf, $section, 'Compiler');
  119.             foreach my $syslib (split ' ', $syslibs) {
  120.                 my $dir = TargetDir($section, "Menu") . "<Libraries>:";
  121.                 push @{$libraries{join('-', '', $arch || (), $compiler || ())}}, "$dir$syslib";
  122.             }
  123.         }
  124.         my $rezs = SCF::Lookup($scf, $section, 'Resources');
  125.         if (defined $rezs) {
  126.             foreach my $rez (split ' ', $rezs) {
  127.                 my $dir = TargetDir($section) . "Resources:";
  128.                 push @resources, "$dir$rez";
  129.             }
  130.         }
  131.     }
  132.     
  133.     my @output;
  134.     foreach my $ext (sort keys %dirs) {
  135.         length $ext or $ext = "NULL";
  136.         push @output, ".SOURCE.$ext : " 
  137.             . join('', '"', join('" "', Unique(@{$dirs{$ext}})), '"', "\n");
  138.     }
  139.     push @output, "\n";
  140.     foreach my $libs (keys %libraries) {
  141.         push @output, "LIBS$libs = ", join(' ', map { qq("$_") } @{$libraries{$libs}}), "\n";
  142.     }
  143.     push @output, 'ALLLIBS = $(LIBS) $(LIBS-$(ARCH)) $(LIBS-$(ARCH)-$(COMPILER))', "\n";
  144.     push @output, "REZS = ", join(' ', map { qq("$_") } @resources), "\n";
  145.     
  146.     push @output, "MORESRCS = ", join(' ', @source), "\n";
  147.     sub I { "-i '$_'" }
  148.     push @output, "CINCLUDES = ", 
  149.         join(' ', (map { I } @includes)), "\n";
  150.     push @output, "CSYSINCLUDES = ", 
  151.         join(' ', (map { I } @sysincludes)), "\n";
  152.     
  153.     return \@output;
  154. }
  155.  
  156. 1;
  157.